package com.cqqs.common.bean;

import com.cqqs.common.enums.error.BusinessError;
import com.cqqs.common.enums.error.CommonErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


/**
 * 底物
 *
 * @author mjxie
 * @date 2022/09/21
 */
@Data
@ApiModel("出参超类")
public class BaseRes<T> implements Serializable {
    private static final long serialVersionUID = 2684928684257208663L;

    /**
     * 返回码
     */
    @ApiModelProperty("返回码")
    private int code;

    /**
     * 返回信息
     */
    @ApiModelProperty("返回信息")
    private String message;

    /**
     * 返回数据
     */
    @ApiModelProperty("返回数据")
    private T data;


    /**
     * 失败的情况下调用这个构造 出入错误码 错误信息
     *
     * @param code
     * @param message
     */
    public BaseRes(int code, String message, String errorData) {
        this.code = code;
        this.message = message;
        this.data = (T) errorData;
    }

    public BaseRes(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 失败的情况下调用这个构造 出入错误码 错误信息
     *
     * @param businessError
     */
    public BaseRes(BusinessError businessError) {
        this.code = businessError.getCode();
        this.message = businessError.getMsg();
        this.data = (T) businessError.getData();
    }


    public BaseRes() {
        this.code = CommonErrorEnum.SUCCESS.getCode();
        this.message = CommonErrorEnum.SUCCESS.getMsg();
    }


    public static BaseRes success() {
        return new BaseRes();
    }

    /**
     * 查询数据成功调用这个构造
     *
     * @param data
     */
    public BaseRes(T data) {
        this.code = CommonErrorEnum.SUCCESS.getCode();
        this.message = CommonErrorEnum.SUCCESS.getMsg();
        this.data = data;
    }

}